package com.gxpt.app.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gxpt.app.AppUtils;
import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.gxpt.app.base.ListBaseAdapter;
import com.gxpt.app.base.SuperViewHolder;
import com.gxpt.app.bean.orderdetail_bean;
import com.gxpt.app.bean.orderlist_bean;
import com.gxpt.app.view.Activity_apply_goods;
import com.gxpt.app.view.Activity_evaluate_goods;
import com.gxpt.app.view.Activity_pay_shopping;
import com.utils.ScreenUtils;
import com.utils.picasso.PicassoUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by Lzx on 2016/12/30.
 */

public class shopping_orderlist_list_Adapter extends ListBaseAdapter<orderlist_bean.orderlist> {

    //列表图片
    int pic_view_width = 0;
    int pic_view_hight = 0;
    Context context;

    public shopping_orderlist_list_Adapter(Context context) {
        super(context);
        this.context = context;
        int Sviewwidth  = ScreenUtils.getScreenWidth(context);//
        pic_view_width  = Sviewwidth / 4;
        pic_view_hight  = pic_view_width/10*9;

    }

    @Override
    public int getLayoutId() {
        return R.layout.item_shopping_orderlist_list;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        orderlist_bean.orderlist item = mDataList.get(position);

        String detail_id = item.detail_id;

        TextView txt_shop_name = holder.getView(R.id.txt_shop_name);
        txt_shop_name.setText(item.shop_name);

        TextView txt_id = holder.getView(R.id.txt_id);
        txt_id.setText(item.id);

        TextView txt_order_id = holder.getView(R.id.txt_order_id);
        txt_order_id.setText(item.order_id);

        TextView txt_status = holder.getView(R.id.txt_status);
        txt_status.setText(AppUtils.getOrderSTUTS(item.status));

        TextView txt_nums = holder.getView(R.id.txt_nums);
        TextView txt_sumprices = holder.getView(R.id.txt_sumprices);

        Button toDel    = holder.getView(R.id.toDel);
        Button toCancel = holder.getView(R.id.toCancel);
        Button toPay    = holder.getView(R.id.toPay);

        Button toEvaluate    = holder.getView(R.id.toEvaluate);
        Button toApply       = holder.getView(R.id.toApply);
        //0=>待付款  3=>已完成 4=>已取消

        LinearLayout finish_do = holder.getView(R.id.finish_do);


        if(item.status ==0 || item.status== 4){//只有未支付或者已取消的订单可以删除
            toDel.setVisibility(View.VISIBLE);
        }else{
            toDel.setVisibility(View.GONE);
        }

        if(item.status ==0){//只有未支付
            toCancel.setVisibility(View.VISIBLE);
        }else{
            toCancel.setVisibility(View.GONE);
        }

        if(item.status ==0){//只有未支付
            toPay.setVisibility(View.VISIBLE);
        }else{
            toPay.setVisibility(View.GONE);
        }

        if(item.status ==3){//已经完成
            finish_do.setVisibility(View.VISIBLE);
        }else{
            finish_do.setVisibility(View.GONE);
        }

        toDel.setTag(Integer.parseInt(item.id));
        toCancel.setTag(Integer.parseInt(item.id));
        toPay.setTag(Integer.parseInt(item.id));

        toDel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent(
                        Constant.ACTION_order_delete,""+v.getTag(),""));

            }
        });

        toCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent(
                        Constant.ACTION_order_cancel,""+v.getTag(),""));

            }
        });

        toEvaluate.setTag(item);
        toEvaluate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                orderlist_bean.orderlist i = (orderlist_bean.orderlist)v.getTag();
                Dialog_detail(i);

            }
        });

        toApply.setTag(R.id.p1_aaa,item);
        toApply.setTag(R.id.p1_bbb,detail_id);
        toApply.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                orderlist_bean.orderlist i = (orderlist_bean.orderlist)v.getTag(R.id.p1_aaa);
                String id = (String)v.getTag(R.id.p1_bbb);
                Dialog_detail_apply(i,id);

            }
        });



        LinearLayout layout_sub = holder.getView(R.id.layout_sub);
        layout_sub.removeAllViews();
        List<orderlist_bean.orderlist.goods_infos> goods = item.goods;

        float price = 0;
        int num=0;

        if(goods != null ){
            if(goods.size()>0){
                for (int a = 0; a < goods.size(); a++) {
                    //////
                    num += goods.get(a).goods_num;
                    price += goods.get(a).goods_num * goods.get(a).sell_price;
                    //////
                    layout_sub.addView(creatListItemView(item.goods.get(a), Integer.parseInt(item.goods.get(a).sku_id)));
                }
            }
        }

        txt_nums.setText(context.getString(R.string.txt_suminfo,""+num));
        txt_sumprices.setText(context.getResources().getString(R.string.f_1)+price);

        Bundle a = new Bundle();
        a.putString("order_id",""+item.order_id);
        a.putString("total_price",""+price);
        a.putString("id",""+item.id);

        toPay.setTag((Bundle)a);
        toPay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle b = (Bundle)v.getTag();
                Intent goView = new Intent();
                goView.setClass(context,Activity_pay_shopping.class);
                goView.putExtra("DATA",b);
                context.startActivity(goView);

            }
        });
    }

    private View creatListItemView(orderlist_bean.orderlist.goods_infos d, int id) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.item_item_shopping_orderlist_list, null);

        ImageView img_thumb= (ImageView)convertView.findViewById(R.id.img_thumb);
        if(d.thumb.isEmpty()){
            PicassoUtils.Picasso_displayImage(context,R.mipmap.ic_launcher,pic_view_width,pic_view_hight,img_thumb);
        }else{
            PicassoUtils.Picasso_displayImage(context, Constant.IP +d.thumb,pic_view_width,pic_view_hight,img_thumb);
        }

        TextView txt_goods_name = (TextView)convertView.findViewById(R.id.txt_goods_name);
        txt_goods_name.setText(d.goods_name);

        TextView txt_sku_name = (TextView)convertView.findViewById(R.id.txt_sku_name);
        txt_sku_name.setText(d.sku_name);

        TextView txt_sell_price = (TextView)convertView.findViewById(R.id.txt_sell_price);
        txt_sell_price.append(""+d.sell_price);


        TextView txt_buy_num = (TextView)convertView.findViewById(R.id.txt_buy_num);
        txt_buy_num.setText("x "+d.goods_num);

        convertView.setId(id);
        return convertView;
    }

    Dialog dialog;
    public void Dialog_detail(orderlist_bean.orderlist item){
        dialog=new Dialog(context,R.style.customDialogStyle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view= LayoutInflater.from(context).inflate(R.layout.dialog_goods_list, null);
        dialog.setContentView(view);

        TextView cancel = (TextView)view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        LinearLayout goods_list_layout2 = (LinearLayout)view.findViewById(R.id.goods_list_layout);

        for(int a=0;a < item.goods.size();a++){

            orderlist_bean.orderlist.goods_infos goods_info = item.goods.get(a);
            goods_list_layout2.addView(creatListItemView(goods_info));
        }


        Window dialogWindow = dialog.getWindow();
        dialogWindow.setWindowAnimations(R.style.commonAnimDialogStyle);
        dialogWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialog.show();

    }

    private View creatListItemView(orderlist_bean.orderlist.goods_infos info) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.item_goods_xiajia_list, null);

        TextView list_id = (TextView)convertView.findViewById(R.id.list_id);
        TextView list_title   = (TextView)convertView.findViewById(R.id.list_title);

        list_id.setText(info.goods_id);
        list_title.setText(info.sku_name);
        convertView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                TextView list_id = (TextView)v.findViewById(R.id.list_id);
//                Logger.d("---"+list_id.getText());
                dialog.dismiss();
                Bundle b = new Bundle();
                b.putString("column_id",""+list_id.getText().toString());

                Intent gonew = new Intent();
                gonew.setClass(context,Activity_evaluate_goods.class);
                gonew.putExtra("DATA",b);
                context.startActivity(gonew);

            }
        });
        return convertView;
    }


    Dialog dialog2;
    public void Dialog_detail_apply(orderlist_bean.orderlist item,String id){
        dialog2=new Dialog(context,R.style.customDialogStyle);
        dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view= LayoutInflater.from(context).inflate(R.layout.dialog_apply_list, null);
        dialog2.setContentView(view);

        TextView cancel = (TextView)view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        LinearLayout goods_list_layout2 = (LinearLayout)view.findViewById(R.id.goods_list_layout);

        for(int a=0;a < item.goods.size();a++){

            orderlist_bean.orderlist.goods_infos goods_info = item.goods.get(a);
            goods_list_layout2.addView(creatListItemView_apply(goods_info,id));
        }


        Window dialogWindow = dialog2.getWindow();
        dialogWindow.setWindowAnimations(R.style.commonAnimDialogStyle);
        dialogWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialog2.show();

    }

    private View creatListItemView_apply(orderlist_bean.orderlist.goods_infos info,String detail_id) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.item_goods_xiajia_list, null);

        TextView list_id = (TextView)convertView.findViewById(R.id.list_id);
        TextView list_id_detail = (TextView)convertView.findViewById(R.id.list_id_detail);
        TextView list_title   = (TextView)convertView.findViewById(R.id.list_title);

        list_id_detail.setText(detail_id);
        list_id.setText(info.goods_id);
        list_title.setText(info.sku_name);
        convertView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                TextView list_id = (TextView)v.findViewById(R.id.list_id);
                TextView list_id_detail = (TextView)v.findViewById(R.id.list_id_detail);
//                Logger.d("---"+list_id.getText());
                dialog2.dismiss();
                Bundle b = new Bundle();
                b.putString("detail_id",""+list_id_detail.getText().toString());
                b.putString("column_id",""+list_id.getText().toString());

                Intent gonew = new Intent();
                gonew.setClass(context,Activity_apply_goods.class);
                gonew.putExtra("DATA",b);
                context.startActivity(gonew);

            }
        });
        return convertView;
    }
}
